Base64
小さな base64
モジュールで、base64 テキストをエンコードまたはデコードします。
注意: 大きなテキストチャンクに対してキャッシングを有効にすることをお勧めします。最大で x2 の最適化が可能です。
Example usage
lua
local base64 = require(".base64")
local str = "This will be encoded"
-- is: "VGhpcyB3aWxsIGJlIGVuY29kZWQ="
local encoded = base64.encode(str)
-- is: "This will be encoded"
local decoded = base64.decode(encoded)
assert(decoded == str)
Module functions
encode()
この関数は、提供された文字列をデフォルトのエンコーダーテーブルを使用してエンコードします。エンコーダーはカスタマイズ可能で、より大きなデータチャンクに対してキャッシュが利用可能です。
- パラメーター:
str
:{string}
エンコードする文字列encoder
:{table}
オプションのカスタムエンコーディングテーブルusecache
:{boolean}
大きな文字列用のオプションのキャッシュ(デフォルトではオフ)
- 戻り値: Base64 エンコードされた文字列
Examples
lua
-- prints: "SGVsbG8gd29ybGQ="
print(base64.encode("Hello world"))
-- customize encoder and allow caching
base64.encode(
"Hello world",
base64.makeencoder(nil, "-"),
true
)
decode()
この関数は、提供された Base64 エンコードされた文字列をデフォルトのデコーダーテーブルを使用してデコードします。デコーダーはカスタマイズ可能で、ここでもキャッシュが利用可能です。
- パラメーター:
str
:{string}
デコードする Base64 エンコードされた文字列decoder
:{table}
オプションのカスタムデコーディングテーブルusecache
:{boolean}
大きな文字列用のオプションのキャッシュ(デフォルトではオフ)
- 戻り値: デコードされた文字列
Examples
lua
-- prints: "Hello world"
print(base64.decode("SGVsbG8gd29ybGQ="))
-- customize decoder and allow caching
base64.decode(
"SGVsbG8gd29ybGQ=",
base64.makedecoder(nil, "-"),
true
)
makeencoder()
新しいエンコーダーテーブルを作成し、encode()
関数の結果をカスタマイズします。
- パラメーター:
s62
:{string}
オプションのカスタム文字 (デフォルトは+
)s63
:{string}
オプションのカスタム文字 (デフォルトは/
)spad
:{string}
オプションのカスタムパディング文字 (デフォルトは=
)
- 戻り値: カスタムエンコーダーテーブル
Examples
lua
-- create custom encoder
local encoder = base64.makeencoder(nil, nil, "~")
-- prints "SGVsbG8gd29ybGQ~" instead of "SGVsbG8gd29ybGQ="
print(base64.encode("Hello world", encoder))
makedecoder()
カスタムエンコードされた custom-encoded base64 文字列をデコードできる新しいデコーダーテーブルを作成します。
- パラメーター:
s62
:{string}
オプションのカスタム文字 (デフォルトは+
)s63
:{string}
オプションのカスタム文字 (デフォルトは/
)spad
:{string}
オプションのカスタムパディング文字 (デフォルトは=
)
- 戻り値: カスタムデコーダーテーブル
Examples
lua
local encoder = base64.makeencoder(nil, nil, "~")
local decoder = base64.makedecoder(nil, nil, "~")
-- "SGVsbG8gd29ybGQ~"
local encoded = base64.encode("Hello world", encoder)
-- prints "Hello world"
print(base64.decode(encoded, decoder))